|
1
|
|
|
function isExpired(storedObject) { |
|
2
|
3 |
|
const now = new Date().getTime(); |
|
3
|
|
|
|
|
4
|
3 |
|
return (now - storedObject.created_at) / 1000 > storedObject.ttl; |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
function Persistent(key, autosave) { |
|
8
|
1 |
|
this.buckets = { |
|
9
|
|
|
default: {} |
|
10
|
|
|
}; |
|
11
|
|
|
|
|
12
|
1 |
|
this.currentBucket = this.buckets.default; |
|
13
|
2 |
|
this.key = key || "star_cache"; |
|
14
|
2 |
|
this.autosave = autosave === undefined ? true : autosave; |
|
15
|
1 |
|
this.defaultTtl = 10; |
|
16
|
1 |
|
this._load(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
Persistent.prototype.setAutosave = function (value) { |
|
20
|
|
|
this.autosave = value; |
|
21
|
|
|
return this; |
|
22
|
|
|
}; |
|
23
|
|
|
|
|
24
|
1 |
|
Persistent.prototype.setDefaultTtl = function (ttl) { |
|
25
|
|
|
this.defaultTtl = parseInt(ttl, 10); |
|
26
|
|
|
}; |
|
27
|
|
|
|
|
28
|
1 |
|
Persistent.prototype._save = function () { |
|
29
|
|
|
localStorage.setItem(this.key, JSON.stringify(this.buckets)); |
|
|
|
|
|
|
30
|
|
|
}; |
|
31
|
|
|
|
|
32
|
1 |
|
Persistent.prototype._load = function () { |
|
33
|
|
|
// debugger; |
|
34
|
1 |
|
try { |
|
35
|
1 |
|
let data = localStorage.getItem(this.key); |
|
|
|
|
|
|
36
|
|
|
data = JSON.parse(data); |
|
37
|
2 |
|
if (null !== data) { |
|
38
|
|
|
this.buckets = data; |
|
39
|
2 |
|
if (!this.buckets.hasOwnProperty("default")) { |
|
40
|
|
|
this.buckets.default = {}; |
|
41
|
|
|
} |
|
42
|
|
|
this.currentBucket = this.buckets.default; |
|
43
|
|
|
} |
|
44
|
|
|
} catch (e) { |
|
45
|
1 |
|
console.log("failed to load data from local storage."); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
1 |
|
Persistent.prototype.has = function (key, checkExpired) { |
|
50
|
7 |
|
if (!this.currentBucket.hasOwnProperty(key)) { |
|
51
|
4 |
|
return false |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
if (true !== checkExpired) { |
|
55
|
3 |
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
const storedObject = this.currentBucket[key]; |
|
59
|
|
|
return !isExpired(storedObject); |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
1 |
|
Persistent.prototype.get = function (key, defaultValue) { |
|
63
|
7 |
|
const returnDefault = () => { |
|
64
|
4 |
|
if (typeof defaultValue === "function") { |
|
65
|
1 |
|
return defaultValue(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
return defaultValue |
|
69
|
|
|
}; |
|
70
|
|
|
|
|
71
|
7 |
|
if (!this.has(key)) { |
|
72
|
4 |
|
return returnDefault(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
const storedObject = this.currentBucket[key]; |
|
76
|
3 |
|
if (isExpired(storedObject)) { |
|
77
|
|
|
delete this.currentBucket[key]; |
|
78
|
|
|
return returnDefault(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
if (Array.isArray(storedObject.value)) { |
|
82
|
|
|
return storedObject.value.slice(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
if (typeof storedObject.value === "object") { |
|
86
|
|
|
return Object.assign({}, storedObject.value); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
return storedObject.value; |
|
90
|
|
|
}; |
|
91
|
|
|
|
|
92
|
1 |
|
Persistent.prototype.set = function (key, value, ttl) { |
|
93
|
2 |
|
const storeObject = { |
|
94
|
|
|
value: value, |
|
95
|
|
|
ttl: ttl || this.defaultTtl, |
|
96
|
|
|
created_at: new Date().getTime() |
|
97
|
|
|
}; |
|
98
|
2 |
|
this.currentBucket[key] = storeObject; |
|
99
|
2 |
|
this.autosave && this._save(); |
|
100
|
|
|
|
|
101
|
2 |
|
return this; |
|
102
|
|
|
}; |
|
103
|
|
|
|
|
104
|
1 |
|
Persistent.prototype.setBucket = function (bucket) { |
|
105
|
2 |
|
if (!this.buckets.hasOwnProperty(bucket)) { |
|
106
|
1 |
|
this.buckets[bucket] = {}; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
this.currentBucket = this.buckets[bucket]; |
|
110
|
2 |
|
return this; |
|
111
|
|
|
}; |
|
112
|
|
|
|
|
113
|
1 |
|
Persistent.prototype.removeBucket = function (bucket) { |
|
114
|
2 |
|
if (this.buckets.hasOwnProperty(bucket)) { |
|
115
|
|
|
delete this.buckets[bucket]; |
|
116
|
2 |
|
this.autosave && this._save(); |
|
117
|
|
|
} |
|
118
|
|
|
this.setBucket("default"); |
|
119
|
|
|
return this; |
|
120
|
|
|
}; |
|
121
|
|
|
|
|
122
|
|
|
export default Persistent; |
|
123
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.